﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;
using System.Text;
using Watson.Core.Tools;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class CustomerInfoList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataShow(GetSearchSql());

                if (SessionHelper.UserRoleGroup != PubEnums.RoleGroup.管理员.ToString())
                {
                    DataControlField colEdit = (DataControlField)this.dgdaoim_sus_plyedr.Columns[9];
                    colEdit.Visible = false;

                    DataControlField colDelete = (DataControlField)this.dgdaoim_sus_plyedr.Columns[10];
                    colDelete.Visible = false;
                } 
            }
        }

        private void DataShow(string searchSql)
        {
            //VwSaleCustomerEntity entity = new VwSaleCustomerEntity();
            //AspNetPager1.RecordCount = entity.GetTotalRecords();
            //this.dgdaoim_sus_plyedr.DataSource = entity.GetAllByQuery(searchSql, "order by id desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            //dgdaoim_sus_plyedr.DataBind();

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());

        }

        private string GetSearchSql()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1 ");
            if (txt公司名称.Text.Trim().Length > 0)
            {
                sb.Append(" and  公司名称 like '%").Append(txt公司名称.Text.Trim()).Append("%'"); ;
            }
            if (txt公司电话.Text.Trim().Length > 0)
            {
                sb.Append(" and  公司电话 like '%").Append(txt公司电话.Text.Trim()).Append("%'"); ;
            }

            return sb.ToString();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());

        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerInfoAdd.aspx", true);
        }


        protected void dgdaoim_sus_plyedr_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //try
            //{
            //    if (e.CommandName.ToString() == "del")
            //    {
            //        if (e.CommandArgument.ToString() != "")
            //        {
            //            if (CheckCanDelete(int.Parse(e.CommandArgument.ToString())))
            //            {
            //                if (new tbSaleCustomerEntity().Delete(int.Parse(e.CommandArgument.ToString())) > 0)
            //                {
            //                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('删除成功。',300,180,'提示',null);</script>");
            //                    DataShow(GetSearchSql());
            //                }
            //                else
            //                {
            //                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            //                }
            //            }
            //            else
            //            {
            //                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('该客户有被系统使用，不可删除',300,180,'提示',null);</script>");
            //            }

            //        }
            //    }
            //}
            //catch (APPException appex)
            //{
            //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('" + appex.Message + "。',300,180,'提示',null);</script>");
            //}
            //catch (Exception ex)
            //{
            //    log4netHelper.error("", ex);
            //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            //}
        }

        protected void dgdaoim_sus_plyedr_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowIndex > -1)
            {
                //如果是绑定数据行 
                if (e.Row.RowType == DataControlRowType.DataRow)
                {


                    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                    {
                        ((LinkButton)e.Row.Cells[10].FindControl("lbtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                    }
                }
            }
        }

        /// <summary>
        /// 检测是不是可以删除。如果被使用则返回false 否则为真。可删除
        /// </summary>
        /// <param name="customerIDs"></param>
        /// <returns></returns>
        public bool CheckCanDelete(int customerID)
        {
            tbProductSaleEntity sale = new tbProductSaleEntity();
            if (sale.GetAllByQuery("CustomerID=" + customerID).Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


    }
}

